package com.test.sort;

public class QuickSort {
    
	public static void main(String[] args) {
		
		int arrLen = (int) Math.round(Math.random() * 10);
		int[] arr = new int[arrLen];
		for (int i = 0; i < arrLen; i++) {
			arr[i] = (int) Math.round(Math.random() * 10);
		}
		for (int i = 0; i < arrLen; i++) {
			System.out.print(arr[i] + ",");
		}

		System.out.println();

		r(arr, 0, arrLen-1);

		for (int i = 0; i < arrLen; i++) {
			System.out.print(arr[i] + ",");
		}
	}

	private static void r(int[] arr, int start, int end) {
		if (end <= start) {
			return;
		}
		int flag = arr[start], flagIndex = start;
		for (int i = start + 1; i <= end; i++) {
			if (arr[i] < flag) {
				for (int j = i; j > flagIndex; j--) {
					int temp = arr[j];
					arr[j] = arr[j - 1];
					arr[j - 1] = temp;
				}
				flagIndex++;
			}
		}
		r(arr, 0, flagIndex-1);
		r(arr, flagIndex+1,end );
	}
}
